package demo;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author lingkang
 * Created by 2024/3/26
 */
public class Demo04 {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(9595);
        while (true) {
            Socket accept = serverSocket.accept();
            new Thread(new ProxyRunnable(accept)).start();
        }
    }

    static class ProxyRunnable implements Runnable {
        private Socket socket;

        public ProxyRunnable(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                InputStream clientIn = socket.getInputStream();
                BufferedReader clientR = new BufferedReader(new InputStreamReader(clientIn));
                String header = "";
                while (true) {
                    String res = clientR.readLine();
                    header += res + "\n";
                    if (res==null || res.length()==0) {
                        // header+="\n ";
                        break;
                    }
                }

                System.out.println(String.format("-------------------接收到请求头---------------\n%s", header));
                Socket proxySocket = new Socket();
                proxySocket.connect(new InetSocketAddress("127.0.0.1", 8080), 10000);

                OutputStream proxyOut = proxySocket.getOutputStream();
                OutputStream clientOut = socket.getOutputStream();

                // 判断请求类型
                if ("CONNECT".equalsIgnoreCase(header.substring(0, 7))) { // https
                    clientOut.write("HTTP/1.1 200 Connection Established\r\n\r\n".getBytes());
                    clientOut.flush();
                } else { // 将http请求头转发到目标服务器
                    proxyOut.write(header.getBytes());
                }

                // 将客户端的请求内容转发到目标服务器
                /*byte[] bytes=new byte[1024];
                int len;
                while ((len=clientIn.read(bytes))!=-1){
                    proxyOut.write(bytes,0,len);
                }*/
                new Thread(new ProxyToClientThread(clientIn, proxyOut)).start();

                // 将目标服务器的相应内容转发给客户端
                InputStream proxyIn = proxySocket.getInputStream();
                while (true) {
                    clientOut.write(proxyIn.read());
                }
                /*while ((len=proxyIn.read(bytes))!=-1){
                    clientOut.write(bytes,0,len);
                }*/
                // proxySocket.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("exit connect");
            }
        }
    }

    static class ProxyToClientThread implements Runnable {
        private InputStream in;
        private OutputStream out;

        public ProxyToClientThread(InputStream in, OutputStream out) {
            this.in = in;
            this.out = out;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    out.write(in.read());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.out.println("close ProxyToClientThread ");
            }
        }
    }
}
